﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DataAccessLayer;

namespace BusinessLogicLayer
{
    public class DelegateController
    {
        DelegateDAL delegateDAL = new DelegateDAL();
        EmailNotificationControl emailController = new EmailNotificationControl();
        
        public int getActingManagerID(String userName)
        {
            Department d = delegateDAL.getDeptInfoByUserName(userName);
            if (d.ActingManagerID == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(d.ActingManagerID);
            }            
        }

        public ActingManager getActingManagerInfo(int amID)
        {
            ActingManager am = delegateDAL.getActingManagerInfoByID(amID);
            return am;
        }

        public List<Employee> getEmpList(String userName)
        {
            return delegateDAL.getEmpListByUserName(userName);
        }
        public int saveToActingManager(int empId, DateTime startDate, String reason)
        {
            int delegationId = delegateDAL.saveToAM(empId, startDate, reason);
            return delegationId;
            
        }

        public int revokeAuthority(int empId)
        {
            int delegationID = delegateDAL.revoke(empId);
            return delegationID;
        }
        public void sendNotificationtToDelegatedPerson(int empId, String userName,int delegationId,String status) //empId = delegated Person, userName = dept head
        {
            ActingManager am = delegateDAL.getDelegationInfo(delegationId);
            Department d = delegateDAL.getDeptInfoByUserName(userName);
            String headEmail = d.Employee.Email;
             String Subject,Body;

             String empEmail = am.Employee.Email;
            if(status=="Delegate")
            {
                Subject= "Authority Delegation";
                Body = "Dear " + d.Employee2.EmployeeName + ",\nThis is to inform you that you have been delegated as Department Head.\nDelegation will start on " +
                    am.StartDate + ".\n" + "The department head's comment is " + "\"" + am.Reason + "\"." +
                    "\nThank you!";
            }
            else
            {
                Subject = "Authority Revoking";
                Body = "Dear " + am.Employee.EmployeeName + ",\nThe delegation of authority will be end on " +
                    am.EndDate + ".\n" + 
                    "\nThank you!";
            }
            
            emailController.sendNotification(empEmail, headEmail, Body, Subject);
        }

        
        
    }
}
